import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import ErrorExcption from 'src/error/ErrorExcption';
import { Repository } from 'typeorm';
import { User } from '../user/entities/user.entity';
import { NewUser } from '../user/entities/user.entity.new';
import { CreateLiveDto } from './dto/create-live.dto';
import { UpdateLiveDto } from './dto/updata-live.dto';
import { Live } from './entities/live.entity';

@Injectable()
export class LiveService {
  constructor(
    @InjectRepository(Live)
    private liveRepository: Repository<Live>,
  ) {}

  async create(createLiveDto: CreateLiveDto, user: NewUser) {
    try {
      await this.liveRepository.save({
        ...createLiveDto,
        userId: user.id,
      });
    } catch (error) {
      throw new ErrorExcption(error, 200);
    }
  }

  async findAll() {
    return await this.liveRepository.find();
  }

  async findOne(id: string) {
    return await this.liveRepository.findOne({
      where: { id: id },
    });
  }

  async update(id: string, updateLiveDto: UpdateLiveDto) {
    return this.liveRepository.update(
      { id },
      {
        ...updateLiveDto,
      },
    );
  }

  async remove(id: string) {
    return await this.liveRepository.delete({ id });
  }
}
